/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import bc.MaterialPrendaFacadeLocal;
import be.MaterialPrenda;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;


/**
 *
 **Copyright  2011 Yury Daniel Zavaleta De la Cruz  
 * Licensed under the Apache License,
 * Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and  limitations under the License.
 */
@ManagedBean
@SessionScoped
public class ManagedBeanMaterialPrenda implements Serializable {
     @EJB
  private  MaterialPrendaFacadeLocal materialPrendaFacade;
private List<MaterialPrenda> lista;
  private List<SelectItem> materialprendasItems = new LinkedList<SelectItem>();
private HashMap<Integer, MaterialPrenda> mymaterialprendas = new HashMap<Integer,MaterialPrenda>();
private MaterialPrenda material;
    public ManagedBeanMaterialPrenda() {
        material = new MaterialPrenda();
    }

    public List<MaterialPrenda> getLista_materiales(){
        try {
            lista = materialPrendaFacade.findAll();
        } catch (Exception e) {
        e.printStackTrace();
        }
    return lista;
    }

    public List<SelectItem> getMaterialprendasItems() {
          lista = new ArrayList<MaterialPrenda>();
        materialprendasItems = new LinkedList<SelectItem>();
        lista = materialPrendaFacade.findAll();
        for(MaterialPrenda p: lista){
       mymaterialprendas.put(p.getIdMaterialPrenda(), p);
            materialprendasItems.add(new SelectItem(p, p.getNombreMaterialPrenda()));
        }
        return materialprendasItems;
    }

    public void setMaterialprendasItems(List<SelectItem> materialprendasItems) {
        this.materialprendasItems = materialprendasItems;
    }

    public HashMap<Integer, MaterialPrenda> getMymaterialprendas() {
        return mymaterialprendas;
    }

    public void setMymaterialprendas(HashMap<Integer, MaterialPrenda> mymaterialprendas) {
        this.mymaterialprendas = mymaterialprendas;
    }

    public MaterialPrenda getTallaprenda(Integer id) {
           return (MaterialPrenda) mymaterialprendas.get(id);

    }

    public String Nuevo(){
    material= new MaterialPrenda();
    return "nuevo_material";
    }

    public MaterialPrenda getMaterial() {
        return material;
    }

    public void setMaterial(MaterialPrenda material) {
        this.material = material;
    }

    public String crear(){
        try {
            materialPrendaFacade.create(material);
        } catch (Exception e) {
        e.printStackTrace();
        }
        return "accesorios";
    }
}
